package net.scrumtool.controller;

import net.scrumtool.model.Collaborator;
import net.scrumtool.model.Product;
import net.scrumtool.technics.DAO;
import net.scrumtool.technics.Utils;
import net.scrumtool.view.EditCollaboratorsProduct;
import net.scrumtool.view.Home;
import net.scrumtool.view.OverviewProduct;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.MessageBox;

public class Cproducts {

	Home view;
	private Product productActif;

	public Cproducts(Home view) {
		this.view = view;
	}

	public void init() {
		view.getGrpProject().setVisible(false);
		view.getTltmEditProduct().setEnabled(false);
		view.getTltmDelProduct().setEnabled(false);
		view.getTltmOpenProduct().setEnabled(false);
		view.getTltmEditCollaboratorsProduct().setEnabled(false);

		view.getTableViewerProducts().setContentProvider(new ArrayContentProvider());
		view.getTableViewerProducts().setLabelProvider(new LabelProvider());
		view.getTableViewerProducts().setInput(DAO.getProducts());
	}

	public void listener(final Collaborator userActif) {

		view.getTableProducts().addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StructuredSelection sel = (StructuredSelection) view.getTableViewerProducts().getSelection();
				setProductActif((Product) sel.getFirstElement());

				view.getTltmEditProduct().setEnabled(true);
				view.getTltmDelProduct().setEnabled(true);
				view.getTltmEditSprints().setEnabled(true);
				view.getTltmOpenProduct().setEnabled(true);
				view.getTltmEditCollaboratorsProduct().setEnabled(true);

				view.getLblGrpProducts().setText("View product");
				view.getGrpProject().setVisible(true);

				view.getTxtNameProject().setEditable(false);
				view.getTxtDescriptionProduct().setEditable(false);

				view.getBtnProducts().setVisible(false);

				view.getTxtNameProject().setText(productActif.getName());
				view.getTxtDescriptionProduct().setText(productActif.getDescription());

			}
		});

		view.getTltmEditCollaboratorsProduct().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				EditCollaboratorsProduct vAddCollaborators = new EditCollaboratorsProduct();
				CeditCollaboratorsProduct cEditCollaborators = new CeditCollaboratorsProduct(vAddCollaborators);
				vAddCollaborators.init();
				cEditCollaborators.init(productActif);
				cEditCollaborators.listener(productActif);
				vAddCollaborators.open();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getTltmAddProduct().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				view.getTxtNameProject().setEditable(true);
				view.getTltmEditProduct().setEnabled(false);
				view.getTltmDelProduct().setEnabled(false);
				view.getTltmOpenProduct().setEnabled(false);
				view.getTltmEditCollaboratorsProduct().setEnabled(false);

				view.getLblGrpProducts().setText("Add product");
				view.getTxtNameProject().setText("");
				view.getTxtNameProject().setMessage("Exemple : My Project");
				view.getTxtDescriptionProduct().setText("");
				view.getBtnProducts().setImage(Utils.getImage(Utils.IMG_ADD));
				view.getBtnProducts().setText("Add this project");
				view.getGrpProject().setVisible(true);

				view.getTxtNameProject().setEditable(true);
				view.getTxtDescriptionProduct().setEditable(true);
				view.getBtnProducts().setVisible(true);
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getTltmEditProduct().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				view.getTxtNameProject().setEditable(false);
				view.getLblGrpProducts().setText("Edit project");
				view.getBtnProducts().setImage(Utils.getImage(Utils.IMG_EDIT));
				view.getBtnProducts().setText("Save this project");
				view.getGrpProject().setVisible(true);
				view.getTxtNameProject().setText(productActif.getName());
				view.getTxtDescriptionProduct().setText(productActif.getDescription());
				view.getTxtNameProject().setEditable(true);
				view.getTxtDescriptionProduct().setEditable(true);
				view.getBtnProducts().setVisible(true);
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getBtnProducts().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if (view.getBtnProducts().getText() == "Add this project") {
					if (view.getTxtNameProject().getText().equals("") || view.getTxtDescriptionProduct().getText().equals("")) {
						Utils.MsgBox(view.getShell(), "Please complete all fields", "information");
					} else {

						boolean nomUtilise = false;
						for (Product product : DAO.getProducts()) {
							if (product.getName().equals(view.getTxtNameProject().getText())) {
								nomUtilise = true;
							}
						}
						if (nomUtilise == true) {
							Utils.MsgBox(view.getShell(), "A project already has this name", "information");
						} else {
							Product product = new Product(view.getTxtNameProject().getText(), view.getTxtDescriptionProduct().getText(), null, null, null);
							DAO.SQLProduct(product, "insert");
							view.getTableViewerProducts().add(product);
							Utils.MsgBox(view.getShell(), "Project is create", "information");
						}
					}
				} else {
					if (view.getTxtDescriptionProduct().getText().equals("")) {
						Utils.MsgBox(view.getShell(), "Please complete all fields", "information");
					} else {
						productActif.setName(view.getTxtNameProject().getText());
						productActif.setDescription(view.getTxtDescriptionProduct().getText());
						DAO.SQLProduct(productActif, "update");
						view.getTableViewerProducts().refresh();
						Utils.MsgBox(view.getShell(), "Project is update", "information");
					}
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getTltmDelProduct().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {

				MessageBox messageBoxproduct = new MessageBox(view.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
				messageBoxproduct.setText("SCRUMTOOL");
				messageBoxproduct.setMessage("Do you really want to delete this project ?");
				int responseProduct = messageBoxproduct.open();
				if (responseProduct == SWT.YES) {
					DAO.SQLProduct(productActif, "delete");
					view.getTableViewerProducts().remove(productActif);
					view.getGrpProject().setVisible(false);
				}

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getTltmOpenProduct().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
					OverviewProduct overview = new OverviewProduct();
					overview.init();
					CoverviewProduct cOverview = new CoverviewProduct(overview);
					cOverview.listener(userActif,productActif);
					cOverview.init(userActif, productActif);
					cOverview.reload(userActif, productActif);
					view.close();
					overview.open();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	public void setProductActif(Product productActif) {
		this.productActif = productActif;
	}

}
